package com.teamone.mobilewatch.util;

import android.util.Log;

import com.teamone.mobilewatch.model.PatientReadings;

/**
 * Class to analyze patient state
 */
public class PatientStateAnalyzer {
	private static final String TAG = "PatientStateAnalyzer";

	/**
	 * Analyzes the readings to identify the patient state
	 * 
	 * @param readings
	 * @return patient state
	 */
	public static PatientState process(PatientReadings readings) {
		
		PatientState state = PatientState.STABLE;
		if (readings == null) {
			state = PatientState.UNDEFINED;
		} else {
			// algorithm to calculate state
			// if any of the readings above the unstable/critical
			// threshold - the condition is unstable/critical
			int bphDelta = Math.abs(UserSettings.getBphNorm()
					- readings.getBph());
			int bplDelta = Math.abs(UserSettings.getBplNorm()
					- readings.getBpl());
			int hrDelta = Math.abs(UserSettings.getHrNorm() - readings.getHr());
			float tDelta = Math.abs(UserSettings.getTNorm() - readings.getT()
					/ 10.0f);

			// check for Unstable
			if (bphDelta >= UserSettings.getBphNorm()
					* UserSettings.getThresholdUnstable() / 100
					|| bplDelta >= UserSettings.getBplNorm()
							* UserSettings.getThresholdUnstable() / 100
					|| hrDelta >= UserSettings.getHrNorm()
							* UserSettings.getThresholdUnstable() / 100
					|| tDelta >= UserSettings.getTNorm()
							* UserSettings.getThresholdUnstable() / 100) {

				state = PatientState.UNSTABLE;
			}
			// check for Critical
			if (bphDelta >= UserSettings.getBphNorm()
					* UserSettings.getThresholdCritical() / 100
					|| bplDelta >= UserSettings.getBplNorm()
							* UserSettings.getThresholdCritical() / 100
					|| hrDelta >= UserSettings.getHrNorm()
							* UserSettings.getThresholdCritical() / 100
					|| tDelta >= UserSettings.getTNorm()
							* UserSettings.getThresholdCritical() / 100) {

				state = PatientState.CRITICAL;
			}

		}

		return state;
	}

}
